<template>
  <div class="AddOrUpdate_main add_container tenantMenu_main main_container user_container">
    <div v-loading="loading" class="main_main">
      <div class="main_main_container">
        <div class="main_main_right">
          <div class="SearchBar_main main_main_search">
            <!-- <div class="main_main_search_item">
              <div class="formItem_header">
                <div class="formItem_header_operate">
                  <a class="formItem_header_operate_items" @click="add">
                    <svg-icon icon-class="新增" />
                    <div class="item_name">新增</div>
                  </a>
                </div>
              </div>
            </div> -->
            <div class="main_main_search_item">
              <el-input v-model="pageData.searchValue" class="search_input" placeholder="请输入订单号或物料编码" clearable @keyup.enter.native="()=>{pageData.pageNum=1; getList()}" @clear="getList" />
              <el-button class="button_icon" icon="el-icon-search" @click.stop="()=>{pageData.pageNum=1; getList()}" />
            </div>
          </div>
          <div ref="table_container" style="margin-top:20px" class="table_container">

            <ux-grid
              ref="table"
              :data="tableData"
              stripe
              border
              :height="tableHeight"
              :tree-config="{children: 'expandDetails'}"
              use-virtual
              width-resize
              show-header-overflow
              show-footer-overflow
              show-overflow
              @selection-change="(val)=>{selectionChange(val, this)}"
            >
              <ux-table-column type="checkbox" width="100" fixed="left" tree-node />
              <ux-table-column field="code" title="销售订单号" width="160" />
              <ux-table-column field="orderByName" title="制单人员" width="100" />
              <ux-table-column field="customerName" title="客户名称" width="100" />
              <ux-table-column field="orderTime" title="订货日期" width="100" />

              <ux-table-column
                title="附件"
                width="80"
              >
                <template v-slot="{ row }">
                  <FileManage
                    v-if="row.isParent==1"
                    :file-names="row.fileName"
                    :file-real-names="row.fileRealName"
                    :file-urls="row.url"
                    :readonly="true"
                    class="accessory"
                  />
                </template>
              </ux-table-column>
              <ux-table-column field="materialCode" title="物料编码" width="140" />
              <ux-table-column field="materialName" title="物料名称" width="120" />
              <ux-table-column field="materialModel" title="规格型号" width="120" />
              <ux-table-column field="materialUnitName" title="物料单位" width="80" />
              <ux-table-column field="materialRemark" title="备注" width="180" />
              <ux-table-column field="orderNumber" title="订单数量" width="80" />
              <ux-table-column field="saleCustomerDeliveryTime" title="客户交期" width="140" />

              <ux-table-column width="160" title="计划数量" field="code" fixed="right">
                <template v-slot="{ row }">
                  <el-input v-if="!row.expandDetails" v-model="row.unDeliverNumber" type="number" />
                </template>
              </ux-table-column>
            </ux-grid>
          </div>
          <div class="addtype-main_bottoms">
            <pagination
              :total="total"
              :page.sync="pageData.pageNum"
              :limit.sync="pageData.pageSize"
              :selected-num="selectionsAll.length"
              @pagination="getList"
            />
          </div>
        </div>
      </div>
    </div>
    <div style="background:white;border-top:1px solid #ECECEC;" class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { saleOrderList as list } from '@/api/saleManage/saleOrder'
export default {
  props: {
    layerid: {},
    selectedRow: {},
    selectedRowAll: {}
  },
  data() {
    return {
      materialTypeId: '',
      typeList: [],
      activeNum: 1,
      deptData: [],
      selections1: [],
      currentDeptId: '',
      currentDeptName: '',
      loading: false,
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'code',
        isAsc: 'desc',
        searchValue: '',
        status: '4,5'
      },
      total: 1, // 数据一共有多少条
      mainId: 'saleOrderDetailId', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      peopleList: [],
      selectArr: [],
      rowShowIndex: 0,
      defaultTableColumns: [
        // 表格常规字段（初始值）
        // { prop: 'code', label: '销售订单号', minWidth: '180' },
        { prop: 'orderByName', label: '制单人员', minWidth: '100' },
        { prop: 'customerName', label: '客户名称', minWidth: '100' },
        { prop: 'orderTime', label: '订货日期', minWidth: '140' },
        { prop: 'materialCode', label: '物料编码', minWidth: '120' },
        { prop: 'materialName', label: '物料名称', minWidth: '120' },
        { prop: 'materialModel', label: '规格型号', minWidth: '120' },
        { prop: 'materialUnitName', label: '物料单位', minWidth: '120' },
        { prop: 'materialRemark', label: '备注', minWidth: '180' },
        { prop: 'orderNumber', label: '订单数量', minWidth: '80' },
        { prop: 'saleCustomerDeliveryTime', label: '客户交期', minWidth: '150' }
        // { prop: 'unDeliverNumber', label: '计划数量', minWidth: '80' }
      ],

      operationData: [
        // 右上角的操作按钮
        { label: '导入', perm: 'system:user:import' },
        { label: '新增', perm: 'system:user:add' },
        { label: '批量删除', perm: 'system:user:delete' },
        { label: '批量导出', perm: 'system:user:export' },
        { label: '页面配置', perm: '' }
      ]
    }
  },
  mounted() {
    console.log('this.selectedRow', this.$deepClone(this.selectedRow))
    if (this.selectedRow) {
      this.selectionsAll = this.selectedRowAll
      this.selectionsAll.forEach(item => {
        item.id = item.saleOrderDetailId
        item.code = item.saleOrderCode
        item.unDeliverNumber = item.planNumber
      })
    }
    this.getList()
  },
  methods: {
    setRowSelect() {
      if (!this.selectionsAll || this.selectionsAll.length == 0) {
        return
      }
      const selectedRow = []
      this.tableData.forEach(item => {
        if (!item.expandDetails || item.expandDetails == null) {
          if (this.selectionsAll.some(item2 => item2.saleOrderDetailId == item.saleOrderDetailId)) {
            selectedRow.push({
              row: item,
              selected: true
            })
          }
        } else {
          item.expandDetails.forEach(item2 => {
            if (this.selectionsAll.some(item3 => item3.saleOrderDetailId == item2.saleOrderDetailId)) {
              selectedRow.push({
                row: item2,
                selected: true
              })
            }
          })
        }
      })
      this.$refs.table.toggleRowSelection(selectedRow)
    },
    getList() {
      this.loading = true
      list({ ...this.pageData, isProductPlan: '0' }).then((res) => {
        const tableData = res.data.map((item) => {
          item.isNotselected = true
          // item.saleOrderDetailId = item.id
          const matchingSelection = this.selectionsAll.find(
            (ele) => item.saleOrderDetailId === ele.saleOrderDetailId
          )
          if (matchingSelection) {
            item.unDeliverNumber = matchingSelection.planNumber
          }
          if (item.expandDetails && item.expandDetails.length > 0) {
            item.isNotselected = false
            item.expandDetails = item.expandDetails.map((olo) => {
              olo.isNotselected = true
              // olo.saleOrderDetailId = olo.id
              const matchingSelection2 = this.selectionsAll.find(
                (ele2) => olo.saleOrderDetailId === ele2.saleOrderDetailId
              )
              if (matchingSelection2) {
                olo.unDeliverNumber = matchingSelection2.planNumber
              }
              return olo
            })
          }
          return item
        })
        this.tableData = tableData
        this.total = res.total
        this.$nextTick(() => {
          this.setRowSelect()
          this.loading = false

          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getMaterialInfo(item) {
      this.materialTypeId = item.id
      this.getList()
    },
    cancel() {
      this.$layer.close(this.layerid)
    },
    setFileName(...data) {
      this.formData.fileName = data[0]
      this.formData.fileRealName = data[1]
      this.formData.url = data[2]
    },
    onSubmit() {
      const cacheData = []
      this.selectionsAll.forEach(item => {
        if (item.isParent && item.isParent == 1 && item.expandDetails && item.expandDetails.length > 0) {
          // this.selectionsAll.splice(item, 1)
        } else {
          const cacheData2 = this.$deepClone(item)
          this.tableData.forEach(ele => {
            if (cacheData2.saleOrderId == ele.saleOrderId) {
              cacheData2.unDeliverNumber = ele.unDeliverNumber
            }
            if (ele.expandDetails && ele.expandDetails.length > 0) {
              ele.expandDetails.forEach(olo => {
                if (olo.saleOrderDetailId == cacheData2.saleOrderDetailId) {
                  cacheData2.unDeliverNumber = olo.unDeliverNumber
                }
              })
            }
          })
          cacheData.push(cacheData2)
        }
      })
      this.$parent.showOrder(cacheData)
      this.$layer.close(this.layerid)
    }
  }
}
</script>

<style lang="less" scoped>
.main_isShowCheck {
  display: flex;
}
/deep/.el-tooltip {
  display: flex !important;
}
/deep/ .SaleOrderCodeColumnClass_expand{
  display: inline-block !important;
  width: 20px !important;
  line-height: 20px !important;
  height: 20px !important;
  text-align: center !important;
  margin-right: 3px !important;
}
/deep/.ux-table .displayNoneClass .cell .el-checkbox__inner{
  display: none
}
.main_main {
  height: calc(100% - 50px);
  margin: 0 !important;
}
.main_main_container {
  width: 100%;
  height: 100%;
  display: flex;

  .main_main_left {
    width: 250px;
    height: calc(100% - 20px);
    padding-right: 20px;
    .title {
      padding: 10px 0;
      border-bottom: 1px solid #387BFC;
      margin-bottom: 10px;
    }
    .main_main_left_top {
      width: 100%;
      height: calc(100% - 80px);
      overflow-y: auto;
      .el-tree {
        background-color: transparent;
      }
      /deep/ .el-tree-node__content:hover {
        background-color: rgba(56, 123, 252, 0.5) !important;
        color: white;
      }
    }
    .main_main_left_bottom {
      width: 100%;
      .main_main_left_bottom_item {
        background-color: rgba(74, 77, 82, 0.2);
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 24px;
      }
      .main_main_left_bottom_item:last-child {
        border-top: 1px solid white;
      }
      .main_main_left_bottom_item:hover {
        background-color: rgba(56, 123, 252, 0.5);
        color: white;
      }
      .active {
        .main_main_left_bottom_item {
          background-color: rgba(56, 123, 252, 1);
          color: white;
        }
      }
    }
  }
  .main_main_right {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;

  }
}
.button{
  width: 100px;
  height: 30px;
  color: #fff;
  background-color: #387bfc;
  border-radius: 4px;
  text-align: center;
  line-height: 30px;
}
.formItem_header{
  justify-content: flex-start !important;
  align-items: flex-start !important;
  height: 40px !important;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
.table_container{
  margin-top: 20px !important;
}
.addtype-main_bottoms{
  padding-right: 20px !important;
  margin: 10px 0;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 0px !important;
}
.main_main{
  margin-right:0 !important;
  padding-top:0 !important;
}
.button_icon{
  background:#387bfc;
  width:30px;
}
.search_input{
  width:200px;
  margin-right:10px
}
.unanimous_class{
  font-size: 12px;color: #387bfc;
  cursor: pointer;
}
/deep/ .row_isTag{
  td{
    background-color: rgba(56, 123, 252,0.5) !important;
  }
}

</style>
